﻿using System;
using System.Globalization;
using System.Xml.Linq;
using FarseerGames.FarseerPhysics.Mathematics;

namespace FreeGoo.LevelClasses
{
    public static class XmlExtensions
    {
        public static string ToLowerInvariant(this string str)
        {
            return str.ToLower(CultureInfo.InvariantCulture);
        }
        
        public static bool ToBool(this XAttribute source, bool defaultValue)
        {
            if (source == null)
            {
                return defaultValue;
            }

            if (source.Value.ToLowerInvariant() == "true")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool ToBool(this XAttribute source)
        {
            return source.ToBool(false);
        }

        public static double ToDouble(this XAttribute attribute, double defaultValue)
        {
            double res;

            if (attribute == null || !double.TryParse(attribute.Value, out res))
            {
                return defaultValue;
            }
            else
            {
                return res;
            }
        }

        public static double ToDouble(this XAttribute attribute)
        {
            return attribute.ToDouble(0);
        }

        public static float ToFloat(this XAttribute attribute, float defaultValue)
        {
            float res;

            if (attribute == null || !float.TryParse(attribute.Value, System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out res))
            {
                return defaultValue;
            }
            else
            {
                return res;
            }
        }

        public static float ToFloat(this XAttribute attribute)
        {
            return attribute.ToFloat(0);
        }

        public static Vector2 ToVector2(this XAttribute attribute, Vector2 defaultValue)
        {
            if (attribute == null)
            {
                return new Vector2(defaultValue.X, defaultValue.Y);
            }

            return attribute.ToVector2();
        }

        public static Vector2 ToVector2(this XAttribute attribute)
        {
            if (attribute == null)
            {
                throw new NullReferenceException("ToVector2 requires an value!");
            }

            Vector2 vector2 = new Vector2(0, 0);
            string[] values = attribute.Value.Split(',');
            vector2.X = float.Parse(values[0], CultureInfo.InvariantCulture);
            vector2.Y = float.Parse(values[1], CultureInfo.InvariantCulture);
            return vector2;
        }

        public static int ToInt(this XAttribute attribute, int defaultValue)
        {
            int res;

            if (attribute == null || !int.TryParse(attribute.Value, System.Globalization.NumberStyles.Any, CultureInfo.InvariantCulture, out res))
            {
                return defaultValue;
            }
            else
            {
                return res;
            }
        }

        public static int ToInt(this XAttribute attribute)
        {
            return attribute.ToInt(0);
        }

        public static string ToString2(this XAttribute attribute, string defaultValue)
        {
            if (attribute == null)
            {
                return defaultValue;
            }
            else
            {
                return attribute.Value;
            }
        }
        
        public static string ToString2(this XAttribute attribute)
        {
            return attribute.ToString2(string.Empty);
        }

        public static bool HasAttribute(this XElement element, XName attributeName)
        {
            return element != null && element.Attribute(attributeName) != null;
        }

        public static string Value(this XAttribute attribute, string defaultValue)
        {
            return attribute == null ? defaultValue : attribute.Value;
        }

        public static bool HasElement(this XElement element, XName attributeName)
        {
            return element != null && element.Element(attributeName) != null;
        }
    }
}
